﻿using Kanban.DALContainer;
using Kanban.IBLL;
using Kanban.IDal;
using Kanban.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kanban.BLL
{
    public class WarehouseWarnService : BaseService<WarehouseWarn>, IWarehouseWarnService
    {
        private readonly IWarehouseWarnDal WarehouseWarnDal = Container.Resolve<IWarehouseWarnDal>();

        public override void SetCurrentDal()
        {
            Dal = WarehouseWarnDal;
        }

        public async Task<Tuple<IList<object>, int>> GetModelsByPageAsync(string search, int pageSize, int pageIndex)
        {
            return await WarehouseWarnDal.GetModelsByPageAsync(search, pageSize, pageIndex);
        }

        public async Task<bool> Save(WarehouseWarn model)
        {
            var query = await GetModelAsync(model.Id);
            if (query == null)
            {
                var isExists = await WarehouseWarnDal.Exists(model);
                if (!isExists)
                {
                    return await AddAsync(model) > 0;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                query.StoreCode = model.StoreCode;
                query.PartNo = model.PartNo;
                query.RePartNo = model.RePartNo;
                query.MinNum = model.MinNum;
                return await UpdateAsync(query) > 0;
            }
        }

        public async Task<int> BatchAdd(List<WarehouseWarn> list)
        {
            var addList = new List<WarehouseWarn>();
            for (int i = 0; i < list.Count; i++)
            {
                var isExists = await WarehouseWarnDal.Exists(list[i]);
                if (!isExists)
                {
                    addList.Add(list[i]);
                }
            }
            var result = await WarehouseWarnDal.BatchAdd(addList);
            return result;
        }

        public async Task<bool> BatchDel(string ids)
        {
            var result = await WarehouseWarnDal.DeleteAsync(ids);
            return result;
        }
    }
}
